package com.xhsj.user.sms.announcement.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhsj.user.hr.system.web.FileController;
import com.xhsj.user.sms.announcement.entity.TbEnterpriseAnnouncement;
import com.xhsj.user.sms.announcement.entity.TbEnterpriseAnnouncementFile;
import com.xhsj.user.sms.announcement.mapper.TbEnterpriseAnnouncementMapper;
import com.xhsj.user.sms.base.BaseService;
import com.xhsj.user.utils.IdGen;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;

/**
 * <p>
 * 企业公告 服务实现类
 * </p>
 *
 * @author suxiaolin
 * @since 2020-06-23
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
//@CacheConfig(cacheNames = "TbEnterpriseAnnouncement")
public class TbEnterpriseAnnouncementService extends BaseService<TbEnterpriseAnnouncementMapper, TbEnterpriseAnnouncement>{
    @Autowired
    private TbEnterpriseAnnouncementMapper dao;

    @Autowired
    private TbEnterpriseAnnouncementFileService tbEnterpriseAnnouncementFileService;

    @Override
    //@CacheEvict(cacheNames="TbEnterpriseAnnouncement", allEntries=true)
    public boolean save(TbEnterpriseAnnouncement entity){
        return super.save(entity);
    }

    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbEnterpriseAnnouncement getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    //@CacheEvict(cacheNames="TbEnterpriseAnnouncement", allEntries=true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

    //@Cacheable(key = "#data",sync = true)
    public IPage<TbEnterpriseAnnouncement> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbEnterpriseAnnouncement entity = JSON.parseObject(JSON.toJSONString(data), TbEnterpriseAnnouncement.class);
        Page<TbEnterpriseAnnouncement> page = new Page(currentPage,pageSize);
        QueryWrapper<TbEnterpriseAnnouncement> wrapper = new QueryWrapper(entity);
        return super.selectPage(page, wrapper);
    }

    @Override
    //@Cacheable(value = "TbEnterpriseAnnouncement",sync = true)
    public List<TbEnterpriseAnnouncement> selectAll() {
        return super.selectAll();
    }


    //分页查询企业公告list包含通过当前用户查看是否接收
    public IPage<Map<String,Object>> findListByUserIdPage(Map<String, Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        Object userId = data.get("userId");

        Page page = new Page(currentPage,pageSize);
        return dao.findListByUserIdPage(page,userId);
    }

    //查看详情接口根据id
    public List<TbEnterpriseAnnouncement> findListById(String id) {
        return dao.findListById(id);
    }

    //企业公告和附件,保存/修改
    public boolean saveAndFile(TbEnterpriseAnnouncement entity) throws Exception {
        entity.setUpdateBy(entity.getCreateBy());
        if(super.save(entity)){
            List<TbEnterpriseAnnouncementFile> tbEnterpriseAnnouncementFileList = entity.getTbEnterpriseAnnouncementFileList();
            if(tbEnterpriseAnnouncementFileList.size()!=0){
                for (TbEnterpriseAnnouncementFile file: tbEnterpriseAnnouncementFileList) {
                    file.setId(IdGen.uuid());
                    file.setAnnouncementId(entity.getId());
                    file.setCreateBy(entity.getCreateBy());
                    file.setUpdateBy(entity.getCreateBy());
                }
            }
            if(!tbEnterpriseAnnouncementFileService.saveBatch(tbEnterpriseAnnouncementFileList)){
                throw new Exception("企业公告附件,保存失败");
            }
        }else {
            throw new Exception("企业公告附件,保存失败");
        }
        return true;
    }

    public IPage<TbEnterpriseAnnouncement> getListByUserId(Map<String, Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }
        Object userId = data.get("userId");
        Page page = new Page(currentPage,pageSize);
        return dao.getListByUserId(page,userId);
    }

    public boolean deleteById(String id) throws Exception {
        if(super.removeById(id)){
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("announcement_id",id);
             if(!tbEnterpriseAnnouncementFileService.remove(queryWrapper)){
                 throw new Exception("企业公告附件,保存失败");
             }
        } else{
            throw new Exception("企业公告附件,保存失败");
        }
        return true;
    }

    //修改公告
    public boolean updateAndFile(TbEnterpriseAnnouncement entity) throws Exception {
        entity.setUpdateBy(entity.getCreateBy());
        entity.setCreateDate(new Date());
        entity.setUpdateDate(new Date());
        //先删后加
        //删除公告
        if (dao.removeById(entity.getId()) == 1){
            if (dao.insert(entity)== 1) {
                //先删除后添加
                tbEnterpriseAnnouncementFileService.removeAnnouncementId(entity.getId());
                List<TbEnterpriseAnnouncementFile> tbEnterpriseAnnouncementFileList = entity.getTbEnterpriseAnnouncementFileList();
                if (tbEnterpriseAnnouncementFileList.size() != 0) {
                    for (TbEnterpriseAnnouncementFile file : tbEnterpriseAnnouncementFileList) {
                        file.setId(IdGen.uuid());
                        file.setAnnouncementId(entity.getId());
                        file.setCreateBy(entity.getCreateBy());
                        file.setUpdateBy(entity.getCreateBy());
                    }
                }
                if (!tbEnterpriseAnnouncementFileService.saveBatch(tbEnterpriseAnnouncementFileList)) {
                    throw new Exception("企业公告附件,删除失败");
                }
            } else {
                throw new Exception("企业公告,修改失败");
            }
    }else {
            throw new Exception("企业公告,修改失败");
        }
        return true;
    }
}
